#!/bin/sh
# ***************************************************************************
# bootstrap : Set up config.mak
# ***************************************************************************
# Copyright (C) 2003-2007 the VideoLAN team
# $Id: bootstrap 24136 2008-01-06 06:25:53Z pdherbemont $
#
# Authors: Christophe Massiot <massiot@via.ecp.fr>
#          Derk-Jan Hartman <hartman at videolan dot org>
#          Felix Kühne <fkuehne@users.sourceforge.net>
# Modifiers : Chan-gu Lee<maidaro@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
# ***************************************************************************

if test "$#" -gt "2" -o "$1" = "-h" -o "$1" = "--help" ; then
  echo "Usage: $0 [host]" >&2
  echo "  Prepare config.mak file." >&2
  exit 1
fi

LANG=C
export LANG
set -e
set -x

BUILD=`gcc -dumpmachine`
if test "$#" -ge "1"; then
  HOST="$1"
else
  HOST="$BUILD"
fi

rm -f config.mak
{
    echo "# Automatically generated by bootstrap."
    echo "# Make changes if you know what you're doing."
} > config.mak

# Check the HAVE_{OS}
if expr "$HOST" : '.*darwin.*' > /dev/null
then
	echo "HAVE_DARWIN_OS = 1" >> config.mak
	echo "HAVE_BSD = 1" >> config.mak
else
	echo "ERROR:\nThis program only works on or for Mac OSX"
	exit 1
fi

case $HOST in
    ppc-darwin)
# Mac OS X < 10.4
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling VLC requires 10.4.x or later" >&2
        exit 1
    ;;
    powerpc-apple-darwin8)
# Mac OS X 10.4 or 10.5 with Xcode 2.5
# Makefile cross compiling is only supported with gcc-4.0
# ENVP defines the oldest environment on which the program will be able to run
		PREFIX="`pwd`/ppc"
        SDK_TARGET=10.4
		CC=gcc-4.0
		CXX=g++-4.0
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        CFLAGS_TUNING=" -arch ppc -mtune=G5"
        EXTRA_CFLAGS=" -isysroot \${MACOSX_SDK} -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
		if expr "$BUILD" : 'i686-apple-darwin.*' > /dev/null ; then
			CROSS_COMPILE="1"
	        EXTRA_LDFLAGS=" -arch ppc -isysroot \${MACOSX_SDK}"
		else
	        EXTRA_LDFLAGS=" -arch ppc -syslibroot \${MACOSX_SDK}"
		fi
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        ;;
    i686-apple-darwin8)
		PREFIX="`pwd`/i386"
        SDK_TARGET=10.4
		CC=gcc-4.0
		CXX=g++-4.0
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -march=pentium-m -mtune=prescott"
        CFLAGS_TUNING=" -march=pentium-m "
        EXTRA_CFLAGS=" -isysroot \${MACOSX_SDK} -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
		if expr "$BUILD" : 'powerpc-apple-darwin.*' > /dev/null ; then
			CROSS_COMPILE="1"
	        EXTRA_LDFLAGS=" -arch i386 -Wl,-isysroot,\${MACOSX_SDK}"
		else
	        EXTRA_LDFLAGS=" -arch i386 -Wl,-syslibroot,\${MACOSX_SDK}"
	    fi
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        ;;
    powerpc-apple-darwin9)
# Mac OS X 10.5 with Xcode 3, specific rules apply
		PREFIX="`pwd`/ppc"
        SDK_TARGET=10.4
		CC=gcc-4.0
		CXX=g++-4.0
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        CFLAGS_TUNING=" -arch ppc -mtune=G5"
        EXTRA_CFLAGS=" -isysroot \${MACOSX_SDK} -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
		if expr "$BUILD" : 'i686-apple-darwin.*' > /dev/null ; then
			CROSS_COMPILE="1"
	        EXTRA_LDFLAGS=" -arch ppc -isysroot \${MACOSX_SDK}"
		else
	        EXTRA_LDFLAGS=" -arch ppc -syslibroot \${MACOSX_SDK}"
		fi
        echo "PATH = /bin:/usr/bin:/usr/local/bin:" >> config.mak
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
        ;;
    i686-apple-darwin9)
		PREFIX="`pwd`/i386"
        SDK_TARGET=10.4
		CC=gcc-4.0
		CXX=g++-4.0
        MIN_LD_VERSION_WITH_TEXT_RELOCATION=81
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        if test `ld -v 2> /dev/stdout | sed -E 's/.*ld64-([0-9]+).*/\1/'` -lt ${MIN_LD_VERSION_WITH_TEXT_RELOCATION}; then
            echo "NO_TEXT_RELOCATION = 1" >> config.mak
        fi
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.4u.sdk" >> config.mak
        CFLAGS_TUNING=" -march=pentium-m -mtune=prescott"
        CFLAGS_TUNING=" -march=pentium-m "
        EXTRA_CFLAGS=" -isysroot \${MACOSX_SDK} -D\${ENVP} -mmacosx-version-min=\${SDK_TARGET}"
		if expr "$BUILD" : 'powerpc-apple-darwin.*' > /dev/null ; then
			CROSS_COMPILE="1"
	        EXTRA_LDFLAGS=" -arch i386 -Wl,-isysroot,\${MACOSX_SDK}"
		else
	        EXTRA_LDFLAGS=" -arch i386 -Wl,-syslibroot,\${MACOSX_SDK}"
	    fi
	    echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
        ;;
    * )
		echo "ERROR:\nThis program only works on or for Mac OSX"
		exit 1
        ;;
esac

echo "BUILD = $BUILD" >> config.mak
echo "HOST = $HOST" >> config.mak
echo "CROSS_COMPILE = $CROSS_COMPILE" >> config.mak

echo "PREFIX = $PREFIX" >> config.mak

echo "CC = ${CC}" >> config.mak
echo "CXX = ${CXX}" >> config.mak
echo "LD = ${LD}" >> config.mak
echo "RANLIB = ${RANLIB}" >> config.mak
echo "AR = ${AR}" >> config.mak
echo "STRIP = ${STRIP}" >> config.mak
echo "EXTRA_CFLAGS = ${CFLAGS_TUNING} ${EXTRA_CFLAGS}" >> config.mak
echo "EXTRA_CPPFLAGS = ${EXTRA_CPPFLAGS} -isystem \$(PREFIX)/include" >> config.mak
echo "EXTRA_LDFLAGS = ${EXTRA_LDFLAGS}" >> config.mak
echo "EXTRA_PATH = ${EXTRA_PATH}" >> config.mak

if wget --version >/dev/null 2>&1; then
    echo "WGET = `which wget` -c" >> config.mak
elif which curl >/dev/null; then
    echo "WGET = `which curl` -L -O" >> config.mak
else
    echo "You need at least wget or curl to fetch the packages." >&2
    exit 1
fi

if test -f /sw/bin/svn; then
        echo "SVN = /sw/bin/svn" >> config.mak
elif test -f /usr/local/bin/svn; then
        echo "SVN = /usr/local/bin/svn" >> config.mak
elif test -f /usr/bin/svn; then
    echo "SVN = /usr/bin/svn" >> config.mak
elif test -f /opt/local/bin/svn; then
    echo "SVN = /opt/local/bin/svn" >> config.mak
elif svn help >/dev/null 2>&1; then
        echo "SVN = `which svn`" >> config.mak
else
        echo "You do not have a subversion client installed." >&2
fi
